/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.core.commands.template;

import org.eclipse.gef.commands.Command;
import org.eclipse.uml2.uml.Classifier;
import org.eclipse.uml2.uml.ClassifierTemplateParameter;
import org.eclipse.uml2.uml.ParameterableElement;
import org.eclipse.uml2.uml.TemplateParameter;

/**
 * 
 */
public class DeleteReferenceFromTemplateParameterCommand extends Command {
	
	/**
	 * ParameterableElement to delete.
	 */
	private ParameterableElement child;
	
	/**
	 * Owner of the parameterable element to delete.
	 */
	private TemplateParameter parent;
	
	// generic properties for all kind of template parameters to recover when undo() is called
	private ParameterableElement parameteredElement = null;
	private ParameterableElement defaultElement = null;
	private ParameterableElement ownedDefaultElement = null;
	// specific properties to recover for ClassifierTemplateParameter
	private Classifier defaultClassifier = null;
	private Classifier constrainingClassifier = null;
	
	/**
	 * Default Constructor.
	 * 
	 * @param child 
	 */
	public DeleteReferenceFromTemplateParameterCommand(TemplateParameter parent, ParameterableElement child) {
		if (child == null) {
			throw new IllegalArgumentException();
		}
		this.child = child;
		this.parent = parent;
		
		// saves properties for undo()
		if (parent instanceof ClassifierTemplateParameter) {
			defaultClassifier = ((ClassifierTemplateParameter)parent).getDefaultClassifier() ;
			constrainingClassifier = ((ClassifierTemplateParameter)parent).getConstrainingClassifier() ;
		}
		parameteredElement = parent.getParameteredElement() ;
		defaultElement = parent.getDefault() ;
		ownedDefaultElement = parent.getOwnedDefault() ;
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.gef.commands.Command#canUndo()
	 */
	/**
	 * 
	 * 
	 * @return 
	 */
	@Override
	public boolean canUndo() {
		return child != null;
	}
	
	public boolean canRedo() {
		return child != null ;
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.gef.commands.Command#execute()
	 */
	/**
	 * 
	 */
	@Override
	@SuppressWarnings("unchecked")
	public void execute() {
		if (parent.getParameteredElement() == child) {
			parent.setParameteredElement(null) ;
			child.setTemplateParameter(null) ;
			if (parent.getOwnedDefault()==null) {
				parent.setOwnedDefault(null) ;
			}
			if (parent instanceof ClassifierTemplateParameter) {
				((ClassifierTemplateParameter)parent).setDefaultClassifier(null) ;
				((ClassifierTemplateParameter)parent).setConstrainingClassifier(null) ;
			}
			parent.setOwnedDefault(null) ;
			parent.setDefault(null) ;
			setLabel("Delete parametered element (from template parameter)") ;
		}
		if (parent.getDefault() == child) {
			parent.setDefault(null) ;
			setLabel("Delete default (from template parameter)") ;
		}
		if (parent instanceof ClassifierTemplateParameter) {
			ClassifierTemplateParameter parentCTP = (ClassifierTemplateParameter)parent ;
			if (parentCTP.getDefaultClassifier()==child) {
				parentCTP.setDefaultClassifier(null) ;
			}
			else if (parentCTP.getConstrainingClassifier()==child) {
				parentCTP.setConstrainingClassifier(null) ;
				setLabel("Delete constraining classifier (from template parameter)") ;
			}
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.gef.commands.Command#redo()
	 */
	/**
	 * 
	 */
	@Override
	public void redo() {
		execute();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.gef.commands.Command#undo()
	 */
	/**
	 * 
	 */
	@Override
	@SuppressWarnings("unchecked")
	public void undo() {
		if (parent instanceof ClassifierTemplateParameter) {
			((ClassifierTemplateParameter)parent).setDefaultClassifier(this.defaultClassifier) ;
			((ClassifierTemplateParameter)parent).setConstrainingClassifier(this.constrainingClassifier) ;
		}
		parent.setParameteredElement(this.parameteredElement) ;
		parent.setDefault(this.defaultElement) ;
		parent.setOwnedDefault(this.ownedDefaultElement) ;
	}
}
